<?php
/**
 * Created by PhpStorm
 * USER MingTingLiang   QQ 915802070
 * Date 2020-12-16-016   11:07
 */


abstract class Manager
{
    public $subject;
    public $myName = ""; //审批者名字
    public $myState = 0;
    public $leader = false;


    public function __construct()
    {
        //读取专题
        $subject = file_get_contents(__DIR__ . "/subject.json");
        if (!empty($subject)) {
            $this->subject = json_decode($subject);
        }else{
            $this->subject->state = 0;
        }
    }

    /**
     * @param bool $leader
     */
    public function setLeader(Manager $leader): void
    {
        $this->leader = $leader;
    }


    public function step($msg)
    {
        //代表审批
        if ($this->subject->state == $this->myState) {
            if ($this->leader) {
                $this->subject->state = $this->leader->myState; //交控制权
                file_put_contents(__DIR__ . "/subject.json", json_encode($this->subject));

            } else {
                echo "审批已经结束";
            }
            echo "{$msg}当前审批者是{$this->myName}";
        } else {
            if ($this->leader) {
                $this->leader->step($msg);
            }
        }
    }
}